package com.net.advancedThread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {

	
	private static class Task implements Runnable
	{

		CyclicBarrier barrier;
		
		Task(CyclicBarrier barrier)
		{
			this.barrier = barrier;
		}
		
		
		@Override
		public void run()
		{
			System.out.println(" waiting   " + Thread.currentThread().getName());
			try {
				barrier.await();
			} catch (InterruptedException | BrokenBarrierException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("crossed   " +Thread.currentThread().getName());
			
		}
		
	}
	
	
	
	



	public static void main(String[] args)
	{
		final CyclicBarrier test = new CyclicBarrier(3,new Runnable()
		{
			
			@Override
			public void run()
			{
				System.out.println("all parts are arrived at barrier lets play........");
				
			}
		});
		
		
		Thread t1 = new Thread(new Task(test),"Thread   1");
		Thread t2 = new Thread(new Task(test),"Thread   2");
		Thread t3 = new Thread(new Task(test),"Thread   3");
		
		t1.start();
		t2.start();
		t3.start();

	}

}
